package com.tencent.mm.modelvoice;


import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;

import com.tevintao.marketingsystem.log.LogManager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MediaRecorder {

    static {
        System.loadLibrary("stlport_shared");
        System.loadLibrary("wechatvoicesilk");
    }

    public MediaRecorder() {
        super();
    }

    public static void getMabi() {

    }


    public native final static int SilkDecInit(int rate, byte[] bytes, int fileLength);

    public native static int SilkDecUnInit();


    public native static int SilkDoDec(byte[] bytes, short len);


//    public native static SilkDoEnc([BS[B[SZ)I
//
//
//    public native static SilkEncInit(III)I
//
//
//    public native static native SilkEncUnInit()I


    public native static int SilkGetEncSampleRate(byte[] bytes);


//    public native static native_init()Z

    public static String lastDecoderPath = "";

    public static boolean getWxCoder(String srcPath, String deCoderPath) {
        lastDecoderPath = deCoderPath;
//        File f = new File("/mnt/sdcard/ms/1.silk");
        File f = new File(srcPath);
        if (!f.exists()) {
            LogManager.getInstance().logError(srcPath + " 不存在");
            return false;
        }
        int fileLength;
        byte[] bytes = new byte[(int) f.length()];
        try {
            FileInputStream fileInputStream = new FileInputStream(f);
            fileLength = fileInputStream.read(bytes);
            LogManager.getInstance().logInfo("read fileLength = " + fileLength);
            fileInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("FileNotFoundException   fileInputStream =  ");
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            LogManager.getInstance().logError("IOException   fileInputStream =  ");
            return false;
        }
        int rate = SilkGetEncSampleRate(bytes);
        LogManager.getInstance().logInfo("rate = " + rate);
        int SilkDecUnInit = SilkDecUnInit();
        LogManager.getInstance().logInfo("SilkDecUnInit = " + SilkDecUnInit);
        int SilkDecInit = SilkDecInit(rate, bytes, fileLength);
        LogManager.getInstance().logInfo("SilkDecInit = " + SilkDecInit);
        int bufferSize = AudioRecord.getMinBufferSize(rate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT);

        int bufferSizeInBytes = 20480;
        AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, rate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, bufferSizeInBytes, AudioTrack.MODE_STREAM);
        audioTrack.play();

        FileOutputStream fileOutputStream = null;
        try {
            File outFile = new File(deCoderPath);
//            File outFile = new File("/mnt/sdcard/ms/2.pcm");
            if (outFile.exists()) {
                boolean delete = outFile.delete();
                LogManager.getInstance().logInfo("删除文件 " + delete);
            }
            try {
                boolean createNewFile = outFile.createNewFile();
                if (!createNewFile) {
                    LogManager.getInstance().logError("createNewFile 创建目标文件失败");
                    return false;
                }
            } catch (IOException e) {
                e.printStackTrace();
                LogManager.getInstance().logError("创建目标文件失败");
                return false;
            }
            fileOutputStream = new FileOutputStream(outFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        byte[] playBuffer = new byte[bufferSize * 2];
        short playLen = (short) (bufferSizeInBytes / 64);
        int resultCode = 1;
        while (resultCode != 0) {
            resultCode = SilkDoDec(playBuffer, playLen);
            LogManager.getInstance().logInfo("resultCode = " + resultCode);
            audioTrack.write(playBuffer, 0, playLen * 2);
            if (resultCode != 0)
                try {
                    if (fileOutputStream != null) {
                        fileOutputStream.write(playBuffer, 0, playLen * 2);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
        int SilkDecUnInitEnd = SilkDecUnInit();
        LogManager.getInstance().logInfo("SilkDecUnInitEnd = " + SilkDecUnInitEnd);
        audioTrack.stop();
        try {
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }


}
